<?php

/**
 * Includes necessary files.
 */
function import_init ()
{
	if (!class_exists ('BBXF_Parse'))
	{
		require_once ('importer/bbxf-parse.php');
		require_once ('importer/bbip.php');
		require_once ('importer/bbip-vanilla.php');
	}
	if (!class_exists ('BBXP'))
	{
		require_once ('exporter/bbxp.php');
		require_once ('exporter/bbxp-vanilla.php');
	}
	require_once ('backpress/bpdb.php');
	require_once ('backpress/functions.core.php');
}

/**
 * Executes all necessary functions to make the importation happen.
 */
function import_main ()
{
	import_init ();

	$user = $_POST['user'];
	$password = $_POST['password'];
	$database = $_POST['database'];
	$host = $_POST['host'];
	$prefix = $_POST['prefix'];
	
	$bbip = new BBIP_Vanilla;
	$bbip->db = new BPDB (array ('name' => $database, 'user' => $user, 'password' => $password, 'host' => $host));
	$bbip->export_lib->db = $bbip->db;
	$bbip->export_lib->initialize_db ($prefix);

	if ('true' == $_POST['users'])
	{
		$bbip->import_users = true;
	}
	if ('true' == $_POST['content'])
	{
		$bbip->import_content = true;
	}
	if ('true' == $_POST['preserve'])
	{
		$bbip->preserve_ids = true;
		if ('true' == $_POST['current'])
		{
			$bbip->preserve_current_user = true;
		}
		if ('true' == $_POST['admins'])
		{
			$bbip->preserve_admins = true;
		}
	}

	$bbip->read_file ($_FILES['import_file']['tmp_name']);
	while ($bbip->file_contents)
	{
		$current = $bbip->find_element ($bbip->file_contents);
		if ('!--' != $current[0] && '?xml' != $current[0] && 'forums_data' != $current[0])
		{
			die ('Invalid top-level element (' . $current[0] . ').');
		}
		$bbip->call_element ($current);
		$bbip->file_contents = $bbip->remove_element ($current[1], $bbip->file_contents);
	}
	$bbip->check_for_duplicates ();

	$bbip->import_prep ();
	if ($bbip->import_users)
	{
		$bbip->insert_users ();
	}
	if ($bbip->import_content)
	{
		$bbip->insert_forums ();
		$bbip->insert_topics ();
	}
	display_import_results ($bbip);
}

/**
 * Displays the results of the importation process.
 *
 * Displays message upon successful importation.  Also displays
 * data that was skipped based on user import options.
 * (Needs prettification.)
 */

function display_import_results ($bbip)
{
?>
	<p>The importation process was completed successfully!</p>
<?php if ($bbip->skipped_data) : ?>
	<p>Based on the options you chose and conflicts with existing data, some data was skipped.  This data will be shown below.  Sorry it's not very pretty yet...</p>
	<pre><?php print_r ($bbip->skipped_data); ?></pre>
<?php
endif;
}

/**
 * Displays the admin import page.
 *
 * Gives a simple explanation of how the import process works and gives
 * users a form to choose their options and provide their file.
 */
function import_page ()
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Vanilla BBXF Importer</title>
	</head>
	<body>

	<h2>Import</h2>
	<?php if ('true' == $_POST['importing']) : import_main (); else : ?>
	<p>Vanilla can import forums data from a BBXF file you provide via the form below.  Such a file can be generated by another Vanilla installation or another forum software with a compatible export feature.</p>
	<p>This file should contain data about users, forums, topics, and posts.  You can select which types of data to import below.</p>
	<form enctype="multipart/form-data" action="" method="post">
		<fieldset>
			<legend>Import File &raquo;</legend>
			<input type="file" name="import_file" />
		</fieldset>
		<fieldset>
			<legend>Options &raquo;</legend>
			<input type="checkbox" name="users" value="true" checked /> Import user data.<br />
			<input type="checkbox" name="content" value="true" checked /> Import forum, topic, and post data.<br />
			<input type="checkbox" name="preserve" value="true" /> Preserve IDs.<br />
			<input type="checkbox" name="current" value="true" checked /> Do not overwrite current user.<br />
			<input type="checkbox" name="admins" value="true" checked /> Do not overwrite keymasters.
		</fieldset>
		<fieldset><legend>MySQL Hostname</legend>
			<input type="text" name="host" />
		</fieldset>
		<fieldset><legend>MySQL Database</legend>
			<input type="text" name="database" />
		</fieldset>
		<fieldset><legend>MySQL Username</legend>
			<input type="text" name="user" />
		</fieldset>
		<fieldset><legend>MySQL Password</legend>
			<input type="password" name="password" />
		</fieldset>
		<fieldset><legend>MySQL Table Prefix</legend>
			<input type="text" name="prefix" />
		</fieldset>
		<!-- Form options. -->
		<p class="submit">
			<input type="submit" name="submit" value="Import Forums Data" />
			<input type="hidden" name="importing" value="true" />
		</p>
	</form>

<?php
endif;
?>
	</body>
</html>
<?php
}

import_page ();

?>
